using System;
using System.Html;
using System.Runtime.CompilerServices;
using Nsb;
using Nsb.Classes;

namespace ThemeChain
{
    [IgnoreNamespace]
    public class ThemeBase
    {
        public static readonly bool IsStaticPage = Window.Location.Pathname.IndexOf("/pages/") != -1;
        public static string RootPath = IsStaticPage ? "../../" : "../";
        public static string AppPath = "App";
        public static string DbPath = IsStaticPage ? Uri.Join(RootPath, AppPath, "db") : "db";
        public static string AppUri = "App";
        public static string ScriptsUri = "Scripts";
        public static string MediaUri = "Scripts/Media";
        public static string MediaImgUri = "Scripts/Media/Img";
        public static string BaseUri = (IsStaticPage ? Uri.Front(Uri.Front(Window.Location.Href)) : Uri.Front(Window.Location.Href)) + "/";
        public static string StdUnits = "px";

        public const string ErrorLogName = "errors";
        public const string EventLogName = "events";

        public const string Hearts = "&hearts;";
        public const string Clubs = "&clubs;";
        public const string Spades = "&spades;";
        public const string Diamonds = "&diams;";
        public const string Smile = "&#9786;";
        public const string Sun = "&#9788;";
        public const string Bullit = "&#9679;";
        public const string DragOrder = "\u2263";
        public const string CheckMark = "\u2714"; // lighter = \u2713 &#10003;
        public const string BallotBox = "\u2610";
        public const string BallotBoxChecked = "\u2611";
        public const string UpMark = "\u25B2";
        public const string DownMark = "\u25BC";
        public const string LeftMark = "\u25C4";
        public const string RightMark = "\u25BA";

        public const string DevPasswordKey = "DevPassword";
        public const string IsCheckedOut = "Cannot save your changes because someone else has this page checked out. Reload this page to check it out and see their changes.";
        public const string UndoKey = "UndoBuffer";
        public static bool GodMode;
        public static bool Reloading;
        public static bool Testing;
        public static bool ShiftKey;
        public static bool DynamicPaging;
        public static bool Publishing;
        public static bool ActivePage;
        public static bool DevServer = (bool)Script.Literal("window.DevServer");
        public static string UserId;
        public static string Email;
        public const string PageActionKey = "PageAction";
        public const string RedirectDataKey = "RedirectData";
        public const string DevServerKey = "DevServer";

        public static string BaseName
        {
            get
            {
                string b = Uri.End(Uri.Front(Window.Location.Href));
                return IsStaticPage ? Uri.End(Uri.Front(b)) : b;
            }
        }

        public static string KeyPrefix
        {
            get { return Uri.End(Uri.Front(Window.Location.Href).Replace("/pages", "")); }
        }

        public static string FnToObject(object v)
        {
            object [] t = (object[]) Type.GetField(v, "_targets");
            if (t != null)
                foreach (object o in t)
                {
                    if (Type.GetScriptType(o) == "function")
                        return o.ToString();
                }
            return v.ToString();
        }

        public static void PageAction(Action fn)
        {
            string a = "(" + FnToObject(fn) + "())";
            Storage.SetSession(PageActionKey, a);
        }

        public static bool IsLocalhost
        {
            get { return (new string[] {"192.168.1.19", "localhost"}).Contains(Window.Location.Hostname); }
        }

        public static string ParseAnchorsToEdit(string txt)
        {
            return txt;
        }

        public static string ParseEditToAnchors(string txt)
        {
            return txt;
        }

        /// <summary>
        /// Reenterant dynamic overrides performed in Master.cs for every new page load
        /// </summary>
        /// <returns></returns>
        [PreserveName, PreserveCase]
        public static void Update()
        {
        }
    }
}